package com.wy2cloud.core.tools;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 字符串工具类
 * @author WYY
 *@version 2015-11-16
 */
public final class StringTools {
	
	/**
	 * 空字符串
	 */
	public final static String Empty="";

	/**
	 * 是否为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str){
		return StringUtils.isEmpty(str);
	}
	
	
	/**
	 * 是否匹配正则
	 * @param url
	 * @param pattern
	 * @return
	 */
	public static boolean IsMatch(String str,String patternStr){
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}
	
	/**
	 * 截取汉字或汉字�?�字符混合串的前n位，如果第n位为双字节字符，则截取前n-1�? 
	 * @param str
	 * @param n
	 * @return
	 */
	public static String leftCut(String str, int n) {
		byte[] b_str = str.getBytes();
		byte[] new_str;
		int k;
		if (b_str.length < n) {
			return str;
		} else {
			if (b_str[n] < 0 && b_str[n - 1] > 0) {
				k = n - 1;
			} else {
				k = n;
			}
			new_str = new byte[k];
			for (int i = 0; i < k; i++) {
				new_str[i] = b_str[i];
			}
			return new String(new_str);
		}
	}
	
	
	/**
	 * 对象转字符串�? 如果为null�?''时，返回''
	 * @param obj
	 * @return
	 */
	public static String toString(Object obj) {
		if (obj == null || obj.toString().equals("")) {
			return "";
		} else {
			String objValue = obj.toString();
			return objValue;
		}
	}
	
	/**
	 * 组合传入字符串数�?
	 * @param objs
	 * @return
	 */
	public static String Join(Object... objs)
	{
		if(objs != null && objs.length > 0)
		{
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < objs.length; i++) {
				sb.append(objs[i] == null ? Empty : objs[i].toString());
			}
			return sb.toString();
		}
		return Empty;
	}
}
